// user.js
export const state = () => ({
  token: '', // 用户登录令牌
  userInfo: {}, // 用户信息对象
  newsData: {}, // 新闻数据对象
  active: [], // 活动数据数组
  notice: [], // 通知数据数组
  loading: false, // 加载状态标识
  key: 0
})
export const mutations = {
  // 设置通知数据
  setkey(state, data) {
    state.key = data
  },
  // 设置通知数据
  setNotice(state, data) {
    state.notice = data
  },
  // 设置新闻数据
  setNewsData(state, data) {
    state.newsData = data
  },
  // 设置活动数据
  setActive(state, data) {
    state.active = data
  },
  // 设置用户token并存储到cookies
  setToken(state, data) {
    this.$cookies.set('token', data)
    state.token = data
  },
  // 设置加载状态
  setLoading(state, status) {
    state.loading = status
  },
  // 重置用户数据，清除cookies和localStorage
  resetData(state) {
    if (process.client) {
      state.token = ''
      state.userInfo = {}
      state.loading = false
      // 统一清理cookie和localStorage
      const itemsToRemove = ['token', 'userInfo']
      itemsToRemove.forEach(item => {
        this.$cookies.remove(item)
      })
      localStorage.clear()
    }
  },
  // 设置用户信息并存储到cookies
  setUserInfo(state, data) {
    this.$cookies.set('userInfo', data)
    state.userInfo = data
  }
}
export const actions = {
  // 获取用户数据
  async getUserInfo({ commit, state }) {
    try {
      commit('setLoading', true)
      const token = state.token || this.$cookies.get('token')
      if (!token) return
      const res = await this.$axios.get(`/api/user/getUserInfo`)
      if (res) {
        commit('setUserInfo', res)
        this.$cookies.set('uuid', res?.ext?.user_code)
      }
    } catch (error) {
      console.error('获取用户信息失败:', error)
    } finally {
      commit('setLoading', false)
    }
  },
  // 用户登录
  async userLogin({ commit }, credentials) {
    try {
      commit('setLoading', true)
      const res = await this.$axios.post(`/api/user/login`, credentials)
      if (res?.userinfo) {
        commit('setToken', res.userinfo.token || '')
        commit('setUserInfo', res.userinfo)
        this.$cookies.set('uuid', res.userinfo?.ext?.user_code)
        return true
      }
      return false
    } catch (error) {
      console.error('登录失败:', error)
      return false
    } finally {
      commit('setLoading', false)
    }
  },
  // 用户退出
  async userLogout({ commit }) {
    try {
      commit('setLoading', true)
      const res = await this.$axios.post(`/api/user/logout`)
      if (res) {
        commit('resetData')
        if (process.client) {
          window.location.reload()
        }
        return true
      }
      return false
    } catch (error) {
      console.error('退出失败:', error)
      return false
    } finally {
      commit('setLoading', false)
    }
  },
  // 获取新闻列表
  async getNewsList({ commit }) {
    try {
      commit('setLoading', true)
      const res = await this.$axios.get('/api/news/index')
      if (res) {
        commit('setNewsData', res)
        return true
      }
      return false
    } catch (error) {
      console.error('获取新闻列表失败:', error)
      return false
    } finally {
      commit('setLoading', false)
    }
  }
}
export const getters = {}
